package com.javaeedev.junit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class SignOnSpringController implements Controller {

    private Service service;

    public void setService(Service service) {
        this.service = service;
    }

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            service.signon(username, password);
            request.getSession().setAttribute("signon", username);
            return new ModelAndView("/welcome");
        }
        catch(RuntimeException e) {
            return new ModelAndView("/signon");
        }
    }

}
